package com.ysx.oa.role.controller;


import com.ysx.oa.role.bean.RoleBean;
import com.ysx.oa.role.dto.RoleDTO;
import com.ysx.oa.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {


    @Autowired
    private RoleService service;


    // http://localhost:8080/kaoqin/role/find.do?id=800200
    @RequestMapping(value="/find.do",method = RequestMethod.GET)
    public Map<String,Object> findById(Integer id){
        Map<String,Object> map = new HashMap();

        RoleBean bean=null;
        try{
            bean = service.findById(id);
        }catch (Exception exception){
            System.out.println(exception);
        }

        map.put("code",200);
        map.put("message","success");
        map.put("data",bean);
        return map;
    }

    // http://localhost:8080/kaoqin/role/add
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Map<String,Object> add(@RequestBody RoleBean bean){
        Map<String,Object> map = new HashMap();
        Integer add = service.add(bean);

        if(add!=0){
            map.put("code",200);
            map.put("message","success");
            return map;
        }else {
            map.put("code",500);
            map.put("message","failure");
            return map;
        }

    }

    // http://localhost:8080/kaoqin/role/findAll.do
    @RequestMapping(value="/findAll.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findAllRole(){
        Map<String,Object> map = new HashMap();

        List<RoleBean> data=null;
        try{
            data = service.findAllRole();
        }catch (Exception exception){
            System.out.println(exception);
        }

        map.put("code",200);
        map.put("message","success");
        map.put("data",data);
        return map;
    }

    // http://localhost:8080/kaoqin/role/delete.do
    @RequestMapping(value = "/delete.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> delete(@RequestBody RoleDTO roleDTO){
        Map<String,Object> map = new HashMap();
        List<Integer> ids = roleDTO.getIds();
        Integer delete = service.delete(ids);
        if (delete > 0) {
            map.put("code",200);
            map.put("message","success");
            return map;
        }else {
            map.put("code",500);
            map.put("message","failure");
            return map;
        }
    }

    // http://localhost:8080/kaoqin/role/update.do
    @RequestMapping(value = "/update.do",method = RequestMethod.POST)
    public Map<String,Object> update(@RequestBody RoleBean bean){
        Map<String,Object> map = new HashMap();
        Integer update = service.update(bean);

        if(update!=0){
            map.put("code",200);
            map.put("message","success");
            return map;
        }else {
            map.put("code",500);
            map.put("message","failure");
            return map;
        }

    }

}
